package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.dto.LoginDTO;
import com.example.springboot.controller.request.AdminPageRequest;
import com.example.springboot.controller.request.LoginRequest;
import com.example.springboot.entity.Admin;
import com.example.springboot.entity.User;
import com.example.springboot.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin  //解决跨域错误
@RestController
@RequestMapping("/admin")
//是rest风格的admincontroller
public class AdminController {

    @Autowired
    IAdminService adminService;

    @PostMapping("/login")
//   登录
    public Result login(@RequestBody LoginRequest request){
        LoginDTO login = adminService.login(request);
        if(login == null){
            return Result.error("用户名或密码错误");
        }
        return Result.success(login);
    }

    //管理员注册
    @PostMapping("/register")//    积分
    public Result register(@RequestBody Admin admin){
        adminService.register(admin);
        return Result.success();
    }

    //    AdminPageRequest传入用户名和电话信息
    @GetMapping("/page")
    public Result page(AdminPageRequest adminPageRequest){
        //        返回数据 Result.success传入一个对象
        return Result.success(adminService.page(adminPageRequest));

    }

//    post put get delete对应增改查删四类操作
    //    新增会员
    @PostMapping("/save")
    public Result save(@RequestBody Admin admin){
        adminService.save(admin);
        return Result.success();
    }

//    更新会员信息
    @PutMapping("/update")
    public Result update(@RequestBody Admin admin){
        adminService.update(admin);
        return Result.success();
    }

    //    获取用户列表
    @GetMapping("/list")
    public Result list(){
        //        返回数据
        List<Admin> admins=adminService.list();
//        System.out.println("admins内容是"+admins);
        return Result.success(admins);

    }


//    根据ID更新用户信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Admin admin =adminService.getById(id);
        return Result.success(admin);

    }

    //删除操作:
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        adminService.deleteById(id);
        return Result.success();
    }

}
